package com.koron.css2.baseConfig.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.GarbageTypeBean;
import com.koron.css2.baseConfig.bean.GarbageTypeQueryBean;
import com.koron.css2.baseConfig.bean.GarbageUniversalBean;
import com.koron.css2.baseConfig.mapper.GarbageMapper;
import com.koron.css2.baseConfig.mapper.GarbageTypeMapper;
import com.koron.css2.baseConfig.mapper.WaterTypeMapper;
import com.koron.css2.baseConfig.queryBean.GarbageHisQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class UserGarbageHisList implements ServerInterface{

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			GarbageHisQueryBean bean = JsonUtils.objectToPojo(req.getData(), GarbageHisQueryBean.class);
			GarbageMapper mapper = factory.getMapper(GarbageMapper.class);
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<GarbageUniversalBean> list = mapper.selectHisList(bean);
			WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);
			GarbageTypeMapper garbageMapper = factory.getMapper(GarbageTypeMapper.class);
			List<GarbageTypeBean> typeList = garbageMapper.list(new GarbageTypeQueryBean());
			for(GarbageUniversalBean garbageBean:list) {				
				garbageBean.setUseWaterTypeName(waterTypeMapper.findWaterTypeById(garbageBean.getUseWaterType()).getWaterTypeName());
				if(typeList!=null && typeList.size() > 0 && StringUtils.isNotBlank(garbageBean.getGarbateType())) {
					for(int i=0;i<typeList.size();i++) {			//设置垃圾费类型
						if(garbageBean.getGarbateType().equals(typeList.get(i).getId()))
							garbageBean.setGarbateType(typeList.get(i).getGarbageTypeName());
					}
				}
				
			}
			info.setData(new PageInfo<>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
